package com.example.administrator.managesystem.activity.my_profile;


import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.activity.firstpage.circle.adapter.HomeIndexAdapter;
import com.example.administrator.managesystem.adapter.MyRecyclerViewAdapter;
import com.example.administrator.managesystem.base.BaseActivity;
import com.example.administrator.managesystem.base.BaseHelper;
import com.example.administrator.managesystem.bean.GoodsListBean;
import com.example.administrator.managesystem.bean.LandListBean;
import com.example.administrator.managesystem.bean.MyShareCircleBean;
import com.example.administrator.managesystem.bean.UploadBean;
import com.example.administrator.managesystem.presenter.firstpage.FirstPagePresenter;
import com.example.administrator.managesystem.presenter.profile.GoodsPresenter;
import com.example.administrator.managesystem.ui.GoodsView;
import com.example.administrator.managesystem.utils.ShareUtil;
import com.example.administrator.managesystem.view.DeleteGoodsDialog;
import com.example.administrator.managesystem.view.UpdateGoodsDialog;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class GoodsListActivity extends BaseActivity implements GoodsView {
    private TextView add_goods,edit_goods,delete;
    private GoodsPresenter goodsPresenter;
    private XRecyclerView recyclerView;
    private MyRecyclerViewAdapter adapter;
    private RelativeLayout choose_all_rl,communicate_list_rl;
    private boolean isEdit;
    private List<GoodsListBean.DataBean> goods_list=new ArrayList<>();
    private DeleteGoodsDialog deleteGoodsDialog;
    private List<Integer> delete_id_list=new ArrayList<>();
    private int loadPage=1;
    private UpdateGoodsDialog updateGoodsDialog;


    @Override
    public void initView() {
        showfloatButton(true,true);
        recyclerView = findViewById(R.id.goods_recycler);
        choose_all_rl=findViewById(R.id.choose_all_rl);
        communicate_list_rl=findViewById(R.id.communicate_list_rl);
        delete=findViewById(R.id.goods_delete);
        add_goods=findViewById(R.id.add_goods_tv);
        edit_goods=findViewById(R.id.edit_tv);
        goodsPresenter=new GoodsPresenter(GoodsListActivity.this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("msg","asdss");
        goodsPresenter.goodsListRequest(ShareUtil.getUserId(GoodsListActivity.this),ShareUtil.getUserToken(GoodsListActivity.this),1,10,0,GoodsListActivity.this);
    }

    @Override
    public void doBusiness() {
        adapter=new MyRecyclerViewAdapter(GoodsListActivity.this,goods_list);
        BaseHelper.setLinearLayoutManagerVertical(GoodsListActivity.this,recyclerView,adapter);
        //0代表刷新，1代表加载更多
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadPage=1;
                goodsPresenter.goodsListRequest(ShareUtil.getUserId(GoodsListActivity.this),ShareUtil.getUserToken(GoodsListActivity.this),1,10,0,GoodsListActivity.this);
                recyclerView.refreshComplete();

            }

            @Override
            public void onLoadMore() {
                goodsPresenter.goodsListRequest(ShareUtil.getUserId(GoodsListActivity.this),ShareUtil.getUserToken(GoodsListActivity.this),loadPage,10,1,GoodsListActivity.this);
                Log.e("msg","loadPage:"+loadPage);
                recyclerView.loadMoreComplete();
            }

        });


        add_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoActivity(AddGoodsActivity.class,false);
            }
        });
        edit_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isEdit){
                    for(int i=0;i<goods_list.size();i++){
                        goods_list.get(i).setCheck(false);
                    }
                    adapter.notifyDataSetChanged();
                    adapter.setVisble(true);
                    edit_goods.setText("取消");
                    choose_all_rl.setVisibility(View.VISIBLE);
                    choose_all_rl.setSelected(false);
                    communicate_list_rl.setVisibility(View.GONE);
                    add_goods.setVisibility(View.GONE);
                    delete.setVisibility(View.VISIBLE);
                    isEdit=true;
                }else{
                    adapter.setVisble(false);
                    edit_goods.setText("编辑");
                    choose_all_rl.setVisibility(View.GONE);
                    communicate_list_rl.setVisibility(View.VISIBLE);
                    add_goods.setVisibility(View.VISIBLE);
                    delete.setVisibility(View.GONE);
                    isEdit=false;
                }

            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteGoodsDialog=new DeleteGoodsDialog(GoodsListActivity.this);
                deleteGoodsDialog.show();
                deleteGoodsDialog.setOnEnterListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getId();
                        goodsPresenter.deleteGoodsRequest(ShareUtil.getUserId(GoodsListActivity.this),ShareUtil.getUserToken(GoodsListActivity.this),delete_id_list,GoodsListActivity.this);

                    }
                });

                deleteGoodsDialog.setOnCancelListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        deleteGoodsDialog.dismiss();
                        adapter.setVisble(false);
                        edit_goods.setText("编辑");
                        choose_all_rl.setVisibility(View.GONE);
                        communicate_list_rl.setVisibility(View.VISIBLE);
                        add_goods.setVisibility(View.VISIBLE);
                        delete.setVisibility(View.GONE);
                    }
                });
            }
        });

        choose_all_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              if(choose_all_rl==v){
              if(choose_all_rl.isSelected()){
                     choose_all_rl.setSelected(false);
                    for(int i=0;i<goods_list.size();i++){
                        goods_list.get(i).setCheck(false);
                    }
                    adapter.notifyDataSetChanged();
                }else{
                    choose_all_rl.setSelected(true);
                    for(int i=0;i<goods_list.size();i++){
                        goods_list.get(i).setCheck(true);
                    }
                    adapter.notifyDataSetChanged();
                }
            }
              }

        });
       adapter.setOnCheckedClickListener(new MyRecyclerViewAdapter.OnCheckedClickListener() {
           @Override
           public void OnCheckedClick(View view, boolean isCheck, int position) {
               if(isCheck){
                   goods_list.get(position).setCheck(false);
                   choose_all_rl.setSelected(false);
               }else{
                   goods_list.get(position).setCheck(true);

               }

               adapter.notifyDataSetChanged();
           }
       });

     adapter.setOnEditClickListener(new MyRecyclerViewAdapter.OnEditClickListener() {
        @Override
      public void onEditClick(View view, final int id) {
         updateGoodsDialog=new UpdateGoodsDialog(GoodsListActivity.this);
            updateGoodsDialog.setCanceledOnTouchOutside(false);
            updateGoodsDialog.show();
            updateGoodsDialog.setOnEnterClickListener(new UpdateGoodsDialog.OnEnterClickListener() {
                @Override
                public void onEnterClick(View view, EditText price, EditText number) {
                    goodsPresenter.updateGoodsRequest(ShareUtil.getUserId(GoodsListActivity.this),ShareUtil.getUserToken(GoodsListActivity.this),id,number.getText().toString(),price.getText().toString(),GoodsListActivity.this);

                }
            });
            updateGoodsDialog.setOnCancelListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateGoodsDialog.dismiss();
                }
            });

    }
});
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_goods_list;
    }

    @Override
    public void showLandGrid(List<LandListBean.DataBean> list) {

    }

    @Override
    public void showUpload(UploadBean.DataBean dataBean) {

    }

    @Override
    public void showDeleteMsg() {
        delete();
        deleteGoodsDialog.dismiss();
        adapter.setVisble(false);
        edit_goods.setText("编辑");
        choose_all_rl.setVisibility(View.GONE);
        communicate_list_rl.setVisibility(View.VISIBLE);
        add_goods.setVisibility(View.VISIBLE);
        delete.setVisibility(View.GONE);
    }



    @Override
    public void showGoodsList(final List<GoodsListBean.DataBean> list,int type) {
        if(list.isEmpty()){
            return;
        }
        if(type==1){
            goods_list.addAll(list);
        }else{
            goods_list.clear();
            goods_list.addAll(list);
        }

        adapter.notifyDataSetChanged();


    }

    @Override
    public void showErrMsg() {

    }

    @Override
    public void showUpdateMsg() {
        goodsPresenter.goodsListRequest(ShareUtil.getUserId(GoodsListActivity.this),ShareUtil.getUserToken(GoodsListActivity.this),1,10,0,GoodsListActivity.this);
        updateGoodsDialog.dismiss();
    }

    //下架商品
    public void delete(){
        for (int i = goods_list.size()-1; i >=0; i--) {
            if (goods_list.get(i).isCheck()) {
                goods_list.remove(i);
            }
        }
        adapter.notifyDataSetChanged();
    }
    //获取删除的商品id
    public void getId(){
        delete_id_list.clear();
        for(int i=0;i<goods_list.size();i++){
            if(goods_list.get(i).isCheck()){
                delete_id_list.add(goods_list.get(i).getSale_id());
            }
        }
    }
}